package com.numbfish.bean_factory.ex_01_start;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

public class Main {
    public static void main(String[] args) {

        DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory();
        BeanFactory container = (BeanFactory)bindViaCode2(beanRegistry);
        FXNewsProvider newsProvider = 
        (FXNewsProvider)container.getBean("djNewsProvider");
        newsProvider.getAndPersistNews();
    }
    public static BeanFactory bindViaCode1(BeanDefinitionRegistry registry)
    {
        AbstractBeanDefinition newsProvider =
        new RootBeanDefinition(FXNewsProvider.class);
        AbstractBeanDefinition newsListener = 
        new RootBeanDefinition(DowJonesNewsListener.class);
        AbstractBeanDefinition newsPersister = 
        new RootBeanDefinition(DowJonesNewsPersister.class);
        // 将bean定义注册到容器中
        registry.registerBeanDefinition("djNewsProvider", newsProvider);
        registry.registerBeanDefinition("djListener", newsListener);
        registry.registerBeanDefinition("djPersister", newsPersister);
        // 指定依赖关系
        // 1. 可以通过构造方法注入方式，参数位置传递
        ConstructorArgumentValues argValues = new ConstructorArgumentValues();
        argValues.addIndexedArgumentValue(0, newsListener);
        argValues.addIndexedArgumentValue(1, newsPersister);
        newsProvider.setConstructorArgumentValues(argValues);
        // 绑定完成
        return (BeanFactory)registry;
    }
    public static BeanFactory bindViaCode2(BeanDefinitionRegistry registry)
    {
        AbstractBeanDefinition newsProvider =
                new RootBeanDefinition(FXNewsProvider.class);
        AbstractBeanDefinition newsListener =
                new RootBeanDefinition(DowJonesNewsListener.class);
        AbstractBeanDefinition newsPersister =
                new RootBeanDefinition(DowJonesNewsPersister.class);
        // 将bean定义注册到容器中
        registry.registerBeanDefinition("djNewsProvider", newsProvider);
        registry.registerBeanDefinition("djListener", newsListener);
        registry.registerBeanDefinition("djPersister", newsPersister);
        // 指定依赖关系

        // 2. 或者通过setter方法注入方式，调用set方法，所以属性名要对应
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("newsListener",newsListener));
        propertyValues.addPropertyValue(new PropertyValue("newsPersister",newsPersister));
        newsProvider.setPropertyValues(propertyValues);
        // 绑定完成
        return (BeanFactory)registry;
    }
}
